<?php
class DataController extends Zend_Controller_Action
{	
    function init()
    {
		$this->initView();		
        $this->view->baseUrl = $this->_request->getBaseUrl();
 		$this->view->test="test";
		Zend_Loader::loadClass('plmorder');
		Zend_Loader::loadClass('google');
		Zend_Loader::loadClass('shopzilla');
		Zend_Loader::loadClass('shopping');
		Zend_Loader::loadClass('product');
		Zend_Loader::loadClass('thefind');
    }
 
   function indexAction() 
    { 

		$this->view->title = 'plima管理系统';
		$this->render();
		//print_r($_SESSION['Auth_Code']);
    } 
 
	function explodeAction()
	{
		$id=$this->_request->getParam('id');
		$config=Zend_Registry::get('config');
		$product=new product($id);
		$category=$this->_request->getParam('category');
		if(isset($category)){
			$accessory=$config->$id->accessory;
			$products=$product->getAll(0,'all',$accessory);
		}		
		else{
			$products=$product->getAll(0,'all');
		}
		//print_r(sizeof($products));exit;
		//print_r($products);
	//	$count=sizeof($products);
//		$s=$j=$k=$m=$y=0;
//		for($i=0;$i<$count;$i++){
//				if(isset($products[$i]['special_price']) || isset($products[$i]['price'])){
//					$s++;
//			}
//			else{
//				$y++;
//				}
//		}
//		echo "总共".$count.'条数据';
//		echo '有优惠价格的有'.$s."条数据";		
//		echo '没有标价格的'.$y.'条数据';
/*	
		for($i=0;$i<$count;$i++){
			echo "第".$i."个"."\r\n";
			if(isset($products[$i]['price'])){
				//print_r($products[$i]['price']);
			}
			else{
				echo "error".$i."errrrrrrrrrrrrrrr";
				}
			}*/
		//print_r($products[0]['price']);
		//$url=$config->$id->url;
		$web_url=$config->$id->web_url;
//		$username=$config->$id->username;
//		$password=$config->$id->password;
//		$plm=new plmorder($url,$username,$password);
//		$products=$plm->getAllProduct();
//		$products=array_slice($products,1,2);
		$type=$this->_request->getParam('type');
		switch($type){
			case "shopzilla":
				header("Content-type:application/txt");
				header('Content-Disposition: attachment; filename="shopzilla_'.$web_url.'.txt"');						
				$shopzilla=new shopzilla($web_url,$products);
				echo $shopzilla->toTxt();
			break;
			case 'shopping':
				  header("Content-type:application/csv");
				  header('Content-Disposition:filename="shopping_'.$web_url.'.csv"');
			      $shopping=new shopping($web_url,$products);
				  echo $shopping->toCsv();
				  //print_r($shopping->toString());
			break;
			case 'thefind':
				header("Content-type:application/txt");
				header('Content-Disposition: attachment; filename="thefind_'.$web_url.'.txt"');				
				$thefind=new thefind($web_url,$products);
				echo $thefind->toTxt();
			break;
			case 'buysafe':
				 $buysafe=new buysafe($web_url,$products);
				 echo $buysafe->toTxt();				 
			break;
			case "google":
				header("Content-type:application/xml");
				header('Content-Disposition:filename="google_'.$web_url.'.xml"');
				$google=new google($web_url,$products);
				echo $google->toXml();
			break;
			default:
				echo "error ";
			break;
			
			}
	}
	
		
	function __call($method,$args){
		if ('Action' == substr($method, -6)) 
    	{            
    		// If the action method was not found, forward to the index action            
    		return $this->_forward('index');        
    	}        
    	// all other methods throw an exception        
    	throw new Exception('Invalid method "' . $method . '" called');    
		}
}
?>